# coding=utf-8
import random

import misc


from base import sg
from common import defines
from table.table_user_info import UserInfo
from mgr.user_mgr import UserMgr
from server_module import misc_logic
from server_module.sender import sender_common
from sql import sqlplus
from sql.database import db
from utils import datetime_util


async def handle(data: dict):
	obj = HandleCommon.ins
	await getattr(obj, data["func"])(data["user_id"], data)

@sg.singleton
class HandleCommon(sg.SingletonBase["HandleCommon"]):
	async def heart(self, user_id, data):
		await UserMgr.ins.heart_user(user_id)
	
	async def recv_ws_test(self, user_id, data):
		value = data["value"]
		await sender_common.send_ws_test(user_id, value)
	

	async def recv_user_nick_name(self, user_id, data):
		user_nick_name = data["user_nick_name"]
		if misc.is_empty(user_nick_name):
			await misc.raise_exception(user_id, "用户昵称为空")

		await sqlplus.update(UserInfo, {
			"id": user_id,
			"user_nick_name": user_nick_name
		})

		await sender_common.send_user_nick_name(user_id, user_nick_name)

